package com.zabra.zl.ui.detailsallocatin;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.AllocationManageBean;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.detailsallocatin.contract.DetailsAllocationContract;
import com.zabra.zl.ui.detailsallocatin.presenter.DetailsAllocationPresenter;
import com.zabra.zl.ui.godeal.GoDealActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.StateUtils;
import com.zabra.zl.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DetailsAllocationActivity extends BaseMvpActivity<DetailsAllocationPresenter> implements DetailsAllocationContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_stockid)
    TextView tvStockid;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_cash)
    TextView tvCash;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_warning)
    TextView tvWarning;
    @BindView(R.id.tv_close)
    TextView tvClose;
    @BindView(R.id.tv_service)
    TextView tvService;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.bt_todeal)
    TextView btTodeal;
    @BindView(R.id.bt_add)
    TextView btAdd;
    @BindView(R.id.bt_supplement)
    TextView btSupplement;
    @BindView(R.id.bt_stop)
    TextView btStop;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    private String strJson, strCash, strTotle, strPay;
    private int bpOrderId;
    private Dialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_details_allocation;
    }

    @Override
    public void initView() {
        strJson = getIntent().getExtras().getString("bean");
        Gson gson = new Gson();
        AllocationManageBean.RespDataBean.DataBean dataBean = gson.fromJson(strJson, AllocationManageBean.RespDataBean.DataBean.class);
        bpOrderId = dataBean.getBpOrderId();
        mPresenter = new DetailsAllocationPresenter();
        mPresenter.attachView(this);
        if (dataBean.getBpType() == 1) {
            //按日
            ivHead.setImageResource(R.mipmap.allocation_day);
        }
        if (dataBean.getBpType() == 2) {
            //按月
            ivHead.setImageResource(R.mipmap.allocation_mon);
        }
        if (dataBean.getBpType() == 3) {
            //免息微策略
            ivHead.setImageResource(R.mipmap.allocation_free);
        }
        if (dataBean.getBpStatus() == 1) {
            //操盘中
            btTodeal.setVisibility(View.VISIBLE);
        } else {
            btTodeal.setVisibility(View.GONE);
        }
        tvId.setText("操盘方案号：" + dataBean.getOrderNo());
        tvDate.setText("申请时间：" + dataBean.getAddTime());
        tvState.setText(StateUtils.getState(dataBean.getBpStatus() + ""));
        //保证金
        strCash = dataBean.getGuaranteeAmt() + "";
        tvCash.setText(strCash);
        //操盘总金额
        strTotle = dataBean.getTotalAmt() + "";
        tvAll.setText(strTotle);
        tvWarning.setText(dataBean.getWarningAmt() + "");
        tvClose.setText(dataBean.getCloseAmt() + "");
        tvService.setText(dataBean.getFee());
        tvStartTime.setText(dataBean.getFormatBeginTime());
        tvEndTime.setText(dataBean.getFormatEndTime());


    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccessAddCash(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            double d = (Double.parseDouble(strPay)) + (Double.parseDouble(strCash));
            tvCash.setText(d + "");
            ToastUtil.showToast(this, bean.getRespDesc());
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else if (bean.getRespCode().equals(Config.NOMONEY)) {
            ToastUtil.showToast(this, bean.getRespDesc());
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccessOver(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            finish();
            ToastUtil.showToast(this, bean.getRespDesc());
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else if (bean.getRespCode().equals(Config.NOMONEY)) {
            ToastUtil.showToast(this, bean.getRespDesc());
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    /**
     * 追加
     */
    private void showDialogAdd() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_add, null);
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
//        WindowManager.LayoutParams params = builder.getWindow().getAttributes();
//        params.width = 800;
        Window window = dialog.getWindow();
//        window.setAttributes(params);
        //去掉白色背景
        window.setBackgroundDrawableResource(android.R.color.transparent);
        //追加保证金
        EditText et_addmoney = view.findViewById(R.id.dialog_addmoney);
        //操盘总金额
        TextView tv_totle = view.findViewById(R.id.dialog_totle);
        //当前保证金
        TextView tv_cash = view.findViewById(R.id.dialog_cash);
        //显示追加保证金
        TextView tv_addcash = view.findViewById(R.id.dialog_addcash);
        //需要支付金额
        TextView tv_pay = view.findViewById(R.id.dialog_pay);
        tv_totle.setText(strTotle);
        tv_cash.setText(strCash);
        et_addmoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                strPay = et_addmoney.getText().toString().trim();
                tv_addcash.setText(strPay);
                tv_pay.setText("共计付款：" + strPay + "元");
            }
        });
        view.findViewById(R.id.iv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.getAddCashData(bpOrderId, strPay);
                dialog.dismiss();
            }
        });
    }

    /**
     * 补亏
     */
    private void showDialogSupplement() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_supplement, null);
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        view.findViewById(R.id.iv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtil.showToast(view.getContext(), "确认补充");
                dialog.dismiss();
            }
        });
    }

    /**
     * 终止
     */
    private void showDialogStop() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_stop, null);
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        view.findViewById(R.id.iv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.getOverDealData(bpOrderId);
                dialog.dismiss();
            }
        });
    }


    @OnClick({R.id.back, R.id.bt_todeal, R.id.bt_add, R.id.bt_supplement, R.id.bt_stop})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.bt_todeal:
                Bundle bundle = new Bundle();
                bundle.putInt("orderid", bpOrderId);
                toClass(view.getContext(), GoDealActivity.class, bundle);
                break;
            case R.id.bt_add:
                showDialogAdd();
                break;
            case R.id.bt_supplement:
                showDialogSupplement();
                break;
            case R.id.bt_stop:
                showDialogStop();
                break;
        }
    }

}
